
if not exists (select 1 from sysobjects where name='mandant' and type='U')
begin
  create table mandant
  (
    mandid            char(2)      not null,
    name              varchar(35)  not null,
    beschreibung      varchar(250) not null,
    constraint pk_mandant primary key (mandid)
  )
end

if not exists (select 1 from sysobjects where name='userrights' and type='U')
begin
  create table userrights
  (
     userid           int             not null identity(1,1),
     username         varchar(35)     not null,
     loginname        varchar(35)     not null,
     password         varbinary(256)  not null,
     beschreibung     varchar(250)    not null,
     userrights        smallint        not null,
     mandid           char(2)         not null,
     constraint pk_userrights primary key (userid,loginname)
  )
  
  alter table userrights
     add constraint fk_userrights_mandant foreign key (mandid) 
      references mandant (mandid) on delete cascade
end


grant select, insert, delete, update on mandant to prsadmins with grant option
grant select, insert, delete, update on userrights to prsadmins with grant option

grant select, insert, delete, update on mandant to prsusers
grant select, insert, delete, update on userrights to prsusers
/*
drop table userrights
drop table mandant
*/